import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/base/BaseController.dart';
import 'package:xin_nuo_pda/bean/lot_action_entity.dart';
import 'package:xin_nuo_pda/bean/lot_entity.dart';
import 'package:xin_nuo_pda/bean/lot_id_entity_entity.dart';
import 'package:xin_nuo_pda/http/api/lot_api.dart';
import 'package:xin_nuo_pda/http/api/qa_api.dart';
import 'package:xin_nuo_pda/utils/string_extension.dart';

import '../bean/arrtibut_balue_entity.dart';
import 'QcTrackOutState.dart';

class QcTrackOutController extends BaseController {
  final QcTrackOutState state = QcTrackOutState();

  @override
  void onInit() {
    super.onInit();
    // initList();
  }

  submit() async {
    if (state.lotEntity == null) {
      Get.snackbar('提示', "未获取到批次信息",
          colorText: Colors.white, backgroundColor: Colors.black87);
      return;
    }

    List<LotIdEntity> data = [];
    LotIdEntity info = LotIdEntity();
    info.lOTID = state.lotEntity!.lOTID ?? '';
    data.add(info);

    List<LotActionEntity> entity = [];
    state.defectList.forEach((element) {
      LotActionEntity mEntity = LotActionEntity();
      mEntity.lOTID = state.lotEntity!.lOTID ?? '';
      mEntity.aCTIONCODE = element.cODE;
      mEntity.aCTIONTYPE = 'Defect';
      mEntity.aCTIONCOMMENT = element.dESCRIPTION;
      List<LotActionQTYLIST> mList = [];
      LotActionQTYLIST mQTY = LotActionQTYLIST();
      mQTY.mAINQTY =
          (element.DEFECTQTY == null ? '0' : element.DEFECTQTY)!.toInt();
      mList.add(mQTY);
      mEntity.qTYLIST = mList;
      entity.add(mEntity);
    });

    List<ArrtibutBalueEntity> arrList = [];
    ArrtibutBalueEntity arr = ArrtibutBalueEntity();
    arr.aTTRIBUTENAME = 'sampleQty';
    arr.aTTRIBUTEVALUE = state.qtyController.text;
    arrList.add(arr);

    Map<String, dynamic> params = {
      'ACTIONTYPE': 'DefaultTrackOut',
      'LOTLIST': data,
      'LOTACTIONLIST': entity,
      'LOTATTRIBUTEVALUELIST' : arrList
    };
    var result = await LotApi.trackOut(canelToken, params: params);
    if (result.ok) {
      Fluttertoast.showToast(
        msg: "出站成功",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
      );

      Get.back();
    }
  }

  void updateSelectDefectValue(LotSCRAPCODELIST str) {
    state.selectDefectValue = str;
    update([state.defectCodeLogicId]);
  }

  void addDefectData() {
    if (state.selectDefectValue == null) {
      Get.snackbar('提示', '请选择缺陷代码');
    } else {
      LotSCRAPCODELIST defectBean = LotSCRAPCODELIST();
      defectBean.cODE = state.selectDefectValue?.cODE ?? '';
      defectBean.dESCRIPTION = state.selectDefectValue?.dESCRIPTION ?? '';
      state.defectList.add(defectBean);
      update([state.defectTableLogicId, state.defectCountLogicId]);
    }
  }

  void updateDefectCount() {
    int count = 0;
    state.defectList.forEach((element) {
      count += int.parse(element.DEFECTQTY ?? '0');
    });
    state.defectCount = count;
    update([state.defectCountLogicId]);
  }

  getLotInfo(String lotID) async {
    Map<String, dynamic> params = {'LOTID': lotID};
    var result = await LotApi.lotInfo(canelToken, params: params);
    if (result.ok) {
      state.lotEntity = result.data;
      state.defectList = (result.data!.DEFECTCODELIST ?? []);
      update([state.defectTableLogicId,state.lotEntityID]);
    }
  }
}
